﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;

namespace DAL
{
    public class PageDAL : SqlDataProvider
    {
        #region[Page_GetById]
        public List<Page> Page_GetById(string Id)
        {
            List<Entity.Page> list = new List<Entity.Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetById", GetConnection()))
            {

                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetByLink]
        public List<Page> Page_GetByLink(string link)
        {
            List<Page> list = new List<Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetByLink", GetConnection()))
            {
                Page obj = new Page();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@link", link));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetBySub]
        public List<Page> Page_GetBySub(string Level)
        {
            List<Page> list = new List<Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetBySub", GetConnection()))
            {
                Page obj = new Page();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Level", Level));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetByCungCap]
        public List<Page> Page_GetByCungCap(string Level)
        {
            List<Page> list = new List<Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetByCungCap", GetConnection()))
            {
                Page obj = new Page();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Level", Level));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetByTop]
        public List<Page> Page_GetByTop(string Top, string Where, string Order)
        {
            List<Entity.Page> list = new List<Entity.Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetByTop", GetConnection()))
            {

                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetByAll]
        public List<Page> Page_GetByAll(string Lang)
        {
            List<Entity.Page> list = new List<Entity.Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetByAll", GetConnection()))
            {

                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_GetByLevel]
        public List<Page> Page_GetByLevel(string Level, string Lang, int LevelLength)
        {
            List<Entity.Page> list = new List<Entity.Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_GetByLevel", GetConnection()))
            {

                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Level", Level));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                dbCmd.Parameters.Add(new SqlParameter("@LevelLength", LevelLength));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_Paging]
        public List<Page> Page_Paging(string CurentPage, string PageSize, string Lang)
        {
            List<Entity.Page> list = new List<Entity.Page>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_Paging", GetConnection()))
            {

                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Page.PageIDataReader(dr));
                    }
                }
                dr.Close();

            }
            return list;
        }
        #endregion
        #region[Page_Insert]
        public bool Page_Insert(Page data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
                dbCmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Type", data.Type));
                dbCmd.Parameters.Add(new SqlParameter("@Link", data.Link));
                dbCmd.Parameters.Add(new SqlParameter("@Target", data.Target));
                dbCmd.Parameters.Add(new SqlParameter("@Position", data.Position));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Page");
            return true;
        }
        #endregion
        #region[Page_Update]
        public bool Page_Update(Page data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
                dbCmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Type", data.Type));
                dbCmd.Parameters.Add(new SqlParameter("@Link", data.Link));
                dbCmd.Parameters.Add(new SqlParameter("@Target", data.Target));
                dbCmd.Parameters.Add(new SqlParameter("@Position", data.Position));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Page");
            return true;
        }
        #endregion
        #region[Page_Delete]
        public bool Page_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Page_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Page");
            return true;
        }
        #endregion

    }
}
